package com.action.pc.index;

import com.alibaba.fastjson.JSONObject;
import com.common.BaseAction;
import com.common.ModelAndJson;
import com.google.common.base.Function;
import com.google.common.collect.Ordering;
import com.service.member.MemberService;
import com.service.order.OrderService;
import com.util.StringUtil;
import org.apache.commons.collections4.MapUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 主页控制器
 *
 * @author 胡涛
 * @date 2018/10/5
 */
@Controller
public class IndexAction extends BaseAction {

    @Autowired
    private MemberService memberService;
    @Autowired
    private OrderService orderService;

    @RequestMapping("main/index")
    public ModelAndView mainindex(ModelMap modelMap,Long timestamp) {
        String time = new DateTime(timestamp).toString("yyyy-MM");
        HashMap<String, Object> paraMap = new HashMap<String, Object>(16);
        filterParaMap(paraMap);
        paraMap.put("time", time);
        Map map = orderService.countOrder(paraMap);
        modelMap.put("map", map);
        modelMap.put("time", time);
        return new ModelAndView("/pc/index", modelMap);
    }
    @RequestMapping("index")
    public ModelAndView index(ModelMap modelMap,Long timestamp) {
        Long jsid = getJsid();
        if (jsid != null) {
            int key = 4 ;
            //合作商
            if (jsid == key) {
                return new ModelAndView("redirect:/order/list.htm", modelMap);
            } else { //超级管理员,城市代理
                modelMap.put("timestamp",timestamp);
                return new ModelAndView("redirect:/main/index.htm", modelMap);
            }
        }else{
            return new ModelAndView("redirect:/login.htm", modelMap);
        }
    }

    @RequestMapping("chartData")
    @ResponseBody
    public JSONObject chartData(ModelAndJson model,String time) {
        HashMap<String, Object> paraMap = new HashMap<String, Object>(16);
        filterParaMap(paraMap);
        if(StringUtil.isEmpty(time)){
            time = new DateTime().toString("yyyy-MM");
        }
        paraMap.put("time",time);
        List<Map> list1 = memberService.countMember(paraMap);
        paraMap.put("isActive", 1);
        List<Map> list2 = memberService.countMember(paraMap);
        HashMap<String, Map> hm = new HashMap<String, Map>(16);
        if (list2 != null) {
            for (int i = 0; i < list2.size(); i++) {
                Map m = list2.get(i);
                String day = MapUtils.getString(m, "days");
                hm.put(day, m);
            }
        }
        if (list1 != null) {
            for (int i = 0; i < list1.size(); i++) {
                Map m = list1.get(i);
                String day = MapUtils.getString(m, "days");
                Map m2 = null;
                if (hm.containsKey(day)) {
                    m2 = hm.get(day);
                } else {
                    m2 = new HashMap(16);
                    m2.put("num", 0);
                    m2.put("days", day);
                }
                m2.put("tot", MapUtils.getIntValue(m, "num"));
                hm.put(day, m2);
            }
        }
        Collection<Map> col = hm.values();
        Ordering<Map> ordering = Ordering.natural().onResultOf(new Function<Map, Comparable>() {
            @Override
            public Comparable apply(Map map) {
                return MapUtils.getString(map, "days");
            }
        });
        List<Map> newList = ordering.sortedCopy(col);
        model.put("data", newList);
        return model.getJson();
    }
}
